package com.zou.application.controller.customer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zou.application.dao.UserMessageEntityMapper;
import com.zou.application.entity.UserMessageEntity;

@Controller
@RequestMapping("/contact")
public class ContactController {
	
	@Autowired
	private UserMessageEntityMapper messageDao;
	
	@RequestMapping("/contactusPage")
	public ModelAndView contactusPage(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("/customer/contact");
		return mv;
	}
	
	@RequestMapping("/contactus")
	public ModelAndView contactus(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("/index");
		String email = request.getParameter("email");
		String message = request.getParameter("message");
		String title = request.getParameter("email");
		String website = request.getParameter("website");
		String userName = request.getParameter("userName");
		try {
			UserMessageEntity messageEntity = new UserMessageEntity();
			messageEntity.setEmail(email);
			messageEntity.setMessage(message);
			messageEntity.setTitle(title);
			messageEntity.setWebsite(website);
			messageEntity.setUserName(userName);
			messageDao.insert(messageEntity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("/messageList")
	public ModelAndView messageList(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("/index");
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			List<UserMessageEntity> messageEntities = messageDao.listByMap(param);
			mv.addObject("messageEntities",messageEntities);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
}
